package com.dog.basics.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.basics.bean.dto.admin.sys.SysDeptAddDto;
import com.dog.basics.bean.dto.admin.sys.SysDeptEditDto;
import com.dog.basics.bean.model.sys.SysDept;
import com.dog.basics.bean.vo.admin.sys.SysDeptChildVo;
import com.dog.basics.bean.vo.admin.sys.SysDeptPageVo;
import com.dog.basics.common.annotation.SysLog;
import com.dog.basics.common.constant.SwaggerConstants;
import com.dog.basics.common.util.R;
import com.dog.basics.service.sys.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统部门
 *
 * @author dog
 */
@RestController
@RequestMapping("/sys/dept")
@Api(value = SwaggerConstants.SYSTEM+ "系统部门模块", tags = SwaggerConstants.SYSTEM+"系统部门模块")
@AllArgsConstructor
@ApiIgnore
public class SysDeptController {

    private final ISysDeptService sysDeptService;


    /**
     * 选择系统根部门列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "选择系统部门列表", notes = "sys:dept:listRootDept")
    @PreAuthorize("@pms.hasPermission('sys:dept:listRootDept')")
    public R<List<SysDeptPageVo>> listRootDept() {
        //查询列表数据
        List<SysDept> deptList = sysDeptService.list(Wrappers.<SysDept>lambdaQuery().eq(SysDept::getPid, 0L)
                .orderByAsc(SysDept::getSort));
        // 数据格式化
        List<SysDeptPageVo> list = deptList.stream().map(sysDept -> {
            SysDeptPageVo sysDeptPageVo = new SysDeptPageVo();
            BeanUtil.copyProperties(sysDept, sysDeptPageVo);
            return sysDeptPageVo;
        }).collect(Collectors.toList());
        return R.ok(list);
    }

    /**
     * 选择系统部门所有列表
     */
    @GetMapping("/listDept")
    @ApiOperation(value = "选择系统部门所有列表", notes = "sys:dept:listDept")
    @PreAuthorize("@pms.hasPermission('sys:dept:listDept')")
    public R<List<SysDeptPageVo>> listDept() {
        //查询列表数据
        List<SysDept> deptList = sysDeptService.list(Wrappers.<SysDept>lambdaQuery().orderByAsc(SysDept::getSort));
        // 数据格式化
        List<SysDeptPageVo> list = deptList.stream().map(sysDept -> {
            SysDeptPageVo sysDeptPageVo = new SysDeptPageVo();
            BeanUtil.copyProperties(sysDept, sysDeptPageVo);
            return sysDeptPageVo;
        }).collect(Collectors.toList());
        return R.ok(list);
    }

    /**
     * 选择子部门
     */
    @GetMapping("/listChildrenDept")
    @ApiOperation(value = "选择子部门", notes = "sys:dept:listChildrenDept")
    @PreAuthorize("@pms.hasPermission('sys:dept:listChildrenDept')")
    public R<List<SysDeptChildVo>> listChildrenDept(Long parentId) {
        //查询列表数据
        List<SysDeptChildVo> deptList = sysDeptService.listChildrenDeptByParentId(parentId);

        return R.ok(deptList);
    }

    /**
     * 部门信息
     */
    @GetMapping("/info/{deptId}")
    @PreAuthorize("@pms.hasPermission('sys:dept:info')")
    @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, paramType = "Long")
    @ApiOperation(value = "部门信息", notes = "sys:dept:info")
    public R<SysDeptChildVo> info(@PathVariable("deptId") Long deptId) {
        SysDept sysDept = sysDeptService.getById(deptId);
        SysDeptChildVo sysDeptChildVo = new SysDeptChildVo();
        BeanUtil.copyProperties(sysDept, sysDeptChildVo);
        return R.ok(sysDeptChildVo);
    }

    /**
     * 保存
     */
    @SysLog("保存部门")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('sys:dept:save')")
    @ApiOperation(value = "保存部门", notes = "sys:dept:save")
    public R<Boolean> save(@Valid @RequestBody SysDeptAddDto sysDeptAddDto) {
        //数据校验
        return R.ok(sysDeptService.saveSysDeptAddDto(sysDeptAddDto));
    }

    /**
     * 修改
     */
    @SysLog("修改部门")
    @PutMapping("/update")
    @PreAuthorize("@pms.hasPermission('sys:dept:update')")
    @ApiOperation(value = "修改部门", notes = "sys:dept:update")
    public R<Boolean> update(@Valid @RequestBody SysDeptEditDto sysDeptEditDto) {
        return R.ok(sysDeptService.updateSysDeptEditDto(sysDeptEditDto));
    }

    /**
     * 删除
     */
    @SysLog("删除部门")
    @DeleteMapping("/delete/{deptId}")
    @PreAuthorize("@pms.hasPermission('sys:dept:delete')")
    @ApiOperation(value = "删除菜单", notes = "sys:dept:delete")
    public R<Boolean> delete(@PathVariable Long deptId) {
        //判断是否有子菜单或按钮
        List<SysDeptChildVo> deptList = sysDeptService.listChildrenDeptByParentId(deptId);
        if (deptList.size() > 0) {
            return R.failed("请先删除子部门");
        }
        sysDeptService.deleteSysDept(deptId);
        return R.ok();
    }


}
